package crm8000.Controllers

import crm8000.services.FidService
import crm8000.services.MainMenuService
import grails.converters.JSON

class HomeController {

	static allowedMethods = [mainMenuItems: 'POST', recentFid: 'POST']
	
	def MainMenuService mainMenuService
	
	def FidService fidService
	
	def index() { }
	
	def error() { }
	
	def navigateTo() {
		def fid = params.fid
		if (!fid) {
			redirect(controller: "home")
		}
		
		switch(fid) {
			default:
			redirect(controller: "home")
		}
	}

	def mainMenuItems() {
		def result = mainMenuService.getMainMenuItems()
		
		render(contentType: "application/json") {
			children = array {
				for (m1 in result.children){
					menu1 caption: m1.caption, fid: m1.fid,
						children:  array {
							for (m2 in m1.children) {
								menu2 caption: m2.caption, fid: m2.fid,
								children: array {
									for (m3 in m2.children) {
										menu3 caption: m3.caption, fid: m3.fid, children: []
									}
								}
							}
						}
				}
			}
		}
	}
	
	def recentFid() {
		def result = fidService.recentFid()
		
		render(contentType: "application/json") {
			root = array {	
					for (r in result){ 
						fid(id: r.fid, name: r.name)
					}
				}
			}
	}
}
